package mya.mes.mdc.logic.status.tag;

import mya.common.jpa.context.ServiceContext;
import mya.common.util.Utils;
import mya.mes.mdc.logic.tag2.ExcelTagAlarmDef;
import mya.mes.mdc.model.status.MdcStatusType;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class StatusTypeUpdater2 {

    public void update(ServiceContext context, List<ExcelTagAlarmDef> tagStatusDefList) {

        List<MdcStatusType> dbList = loadAll(context);

        if (Utils.isNullOrEmpty(tagStatusDefList)) {

            if (Utils.isNullOrEmpty(dbList)) {
                return;
            }

            for (MdcStatusType statusType : dbList) {
                statusType.setActive(Boolean.FALSE);
            }

            return;
        }

        TreeMap<Long, MdcStatusType> dbMap = new TreeMap<>();

        for (MdcStatusType dbStatusType : dbList) {
            dbMap.put(dbStatusType.getObjectId(), dbStatusType);
        }

        List<MdcStatusType> list = buildStatusType(tagStatusDefList);
        try {
            for (MdcStatusType statusType : list) {
                MdcStatusType dbStatusType = dbMap.get(statusType.getObjectId());

                if (dbStatusType != null) {
                    //更新
                    copySrcToDest(statusType, dbStatusType);

                    dbMap.remove(statusType.getObjectId());
                } else {
                    //新增
                    context.em.persist(statusType);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        dbMap.values().forEach(dbStatusType -> {
            dbStatusType.setActive(Boolean.FALSE);
        });

    }

    private void copySrcToDest(MdcStatusType src, MdcStatusType dest) {

//
//
//        public String enUs;

        dest.setEnUs(src.getEnUs());
//
//        public String zhCN;

        dest.setZhCN(src.getZhCN());
    }

    private List<MdcStatusType> loadAll(ServiceContext context) {
        List<MdcStatusType> list =
                context.em.createQuery("select o from MdcStatusType o ", MdcStatusType.class).getResultList();

        return list;
    }

    @SuppressWarnings("all")
    private List<MdcStatusType> buildStatusType(List<ExcelTagAlarmDef> tagStatusDefList) {
        List<MdcStatusType> list = new ArrayList<>(tagStatusDefList.size());


        String rdbTag = null;
        String prefix = null;
        Integer tagIndex = null;
        for (ExcelTagAlarmDef tag : tagStatusDefList) {

            if (tag.rdbTag != null && !tag.rdbTag.isEmpty()) {
                if (!tag.rdbTag.equals(rdbTag)) {
                    rdbTag = tag.rdbTag;

                    prefix = rdbTag.substring(0, 2).toUpperCase();

                    tagIndex = Integer.parseInt(rdbTag.substring(2));
                }
            }

            if (rdbTag == null) {
                continue;
            }
            MdcStatusType statusType = new MdcStatusType();
            statusType.setActive(Boolean.TRUE);


            statusType.setMdcTag(rdbTag);
            statusType.setTagPrefix(prefix);
            statusType.setTagIndex(tagIndex);

            statusType.setBitIndex(tag.bit);


            statusType.setEnUs(tag.enUs);
            statusType.setZhCN(tag.zhCN);


            long objectId = 1_0000_00L + tagIndex * 100 + tag.bit;
            statusType.setObjectId(objectId);

            list.add(statusType);
        }

        return list;
    }


}
